package com.lagou.edu.mvcframework.interceptor;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.logging.Logger;

/**
 * @author liuc
 * @date 2021/4/22
 * @description 异常处理拦截器
 **/
public abstract class ExceptionInterceptor implements ExceptionHandler{

    protected Logger log = Logger.getLogger(ExceptionInterceptor.class.getName());
    protected final String DEFAULT_MSG = "server occur exception !";

    @Override
    public void handleException(HttpServletRequest request, HttpServletResponse response, Exception throwable) throws Exception {
        log.info("======= exception handle ========");
        String msg = message(request,response,throwable);
        msg = StringUtils.isBlank(msg) ? DEFAULT_MSG : msg;
        response.getWriter().write(msg);
    }

    /**
     * 返回一个自定义的异常信息
     * @param request
     * @param response
     * @return
     */
    protected abstract String message(HttpServletRequest request, HttpServletResponse response,Exception throwable);
}
